import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Label;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.ScrollPaneConstants;

public class DManagerFrame extends JFrame {

	private JPanel contentPane;
	private JButton btnAddDownload;
	private JScrollPane scrollPane;
	private JPanel container = new JPanel();
	public static final int FRAME_PREF_WIDTH = 440;
	public static final int FRAME_PREF_HEIGHT = 229;
	public static final int DLCOM_PREF_HEIGHT = 30;
	public static final Dimension DLCOM_PREF_DIM = new Dimension(FRAME_PREF_WIDTH, DLCOM_PREF_HEIGHT);
	public static final int LABEL_PREF_WIDTH = 100;
	
	public static final Icon pauseIcon = new ImageIcon("pause.gif");
	public static final Icon removeIcon = new ImageIcon("remove.gif");
	private final ActionListener listener;
	
	/**
	 * Create the frame.
	 */
	public DManagerFrame(ActionListener incomeDLlistener) {
		listener = incomeDLlistener;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		btnAddDownload = new JButton("add download");
		container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
		container.add(Box.createVerticalGlue());
		scrollPane = new JScrollPane(container);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(btnAddDownload)
					.addContainerGap(305, Short.MAX_VALUE))
				.addComponent(scrollPane, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, FRAME_PREF_WIDTH, GroupLayout.PREFERRED_SIZE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(btnAddDownload)
					.addPreferredGap(ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, FRAME_PREF_HEIGHT, GroupLayout.PREFERRED_SIZE))
		);
		contentPane.setLayout(gl_contentPane);
		this.setResizable(false);
		initEventHandlers();
	}

	private void initEventHandlers() {
		btnAddDownload.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = JOptionPane.showInputDialog(DManagerFrame.this,
						"Download URI: ", "", 1);
				if (str != null) {
					Download dl = new Download(str, container, scrollPane);
					listener.actionPerformed(new ActionEvent(dl, Main.ADDED_CMD, str));
					container.remove(container.getComponentCount() - 1);
					container.add(dl);
					container.add(Box.createVerticalGlue());
					scrollPane.setViewportView(container);
					//scrollPane.setViewportView(new Label(str));
				}
			}
		});
	}
	
}






























